package com.javaclimb.drug.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.javaclimb.drug.common.ResultMapUtil;
import com.javaclimb.drug.common.UploadUtils;
import com.javaclimb.drug.entity.Imginfo;
import com.javaclimb.drug.entity.UploadInfo;
import com.javaclimb.drug.service.IImginfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 图库的controller
 */
@Controller
@RequestMapping(value = "/img")
public class ImginfoController {

    @Autowired
    private IImginfoService iImginfoService;

    /**
     * 图库页面
     */
    @RequestMapping
    public String imgInfo(){
        return "/imgInfo";
    }

    /**
     * 分页查询供应商列表
     */
    @RequestMapping(value = "/imgQueryPage")
    @ResponseBody
    public Object ImginfoQueryPage(String param, @RequestParam(defaultValue = "1")int pageNum,@RequestParam(defaultValue = "10")int pageSize){
        try{
            IPage<Imginfo> iPage = iImginfoService.selectImginfoPage(pageNum,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(iPage);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向供应商新增页面
     */
    @RequestMapping(value = "/imgInfoPage")
    public String ImginfoPage(){
        return "/imgInfoPage";
    }

    /**
     * 添加一个图片数据
     */
    @RequestMapping(value = "/imgInfoAdd")
    @ResponseBody
    public Object ImginfoAdd(Imginfo imginfo){
        try{
            imginfo.setContsign("相似度");
            imginfo.setKeywords("关键字");
            imginfo.setStatus(1);
            imginfo.setCreatetime(new Date());
            imginfo.setUpdatetime(new Date());
            int i = iImginfoService.addImginfo(imginfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向供应商编辑页面
     */
    @RequestMapping(value = "/imgInfoQueryById")
    public String imgInfoQueryById(@RequestParam(name = "id",required = true)Integer id, Model model){
        Imginfo Imginfo = iImginfoService.queryImginfoById(id);
        model.addAttribute("obj",Imginfo);
        return "/imgInfoPage";
    }

    /**
     * 修改一个供应商
     */
    @RequestMapping(value = "/imgInfoEdit")
    @ResponseBody
    public Object imgInfoEdit(Imginfo imginfo){
        try{
            imginfo.setUpdatetime(new Date());
            int i = iImginfoService.editImginfo(imginfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 修改图片状态
     */
    @RequestMapping(value = "/imgInfoUpdateById")
    @ResponseBody
    public Object imgInfoUpdateById(Integer id){
        try{
            Imginfo imginfo = iImginfoService.queryImginfoById(id);
            if(imginfo.getStatus()==0){
                imginfo.setStatus(1);
            }else{
                imginfo.setStatus(0);
            }
            int i = iImginfoService.editImginfo(imginfo);
            return ResultMapUtil.getHashMapSave(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }
    /**
     * 删除一个供应商
     */
    @RequestMapping(value = "/imgInfoDelById")
    @ResponseBody
    public Object imgInfoDelById(Integer id){
        try{
            int i = iImginfoService.delImginfoById(id);
            return ResultMapUtil.getHashMapDel(i);
        } catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有供应商
     */
    @RequestMapping(value = "/imgInfoList")
    @ResponseBody
    public Object imgInfoList(){
        List<Imginfo> imginfoList = iImginfoService.queryImginfoList();
        return ResultMapUtil.getHashMapList(imginfoList);
    }

    /**
     * 上传
     * @param file
     * @return
     */
    @RequestMapping(value = "/upload")
    @ResponseBody
    public Object upload(MultipartFile file){
        UploadInfo uploadInfo = UploadUtils.upload(file);
        HashMap<String,String> map = new HashMap<String, String>();
        map.put("src",uploadInfo.getFileServerPath());
        map.put("path",uploadInfo.getFilePath());
        map.put("filename",uploadInfo.getFileName());
        if(uploadInfo.getFileServerPath() != null){
            return ResultMapUtil.getHashMapUpload(1,map);
        }else {
            return ResultMapUtil.getHashMapUpload(0,map);
        }

    }

}





















